package com.soft.mainsite.module.category.beetl;

import java.util.List;

import com.soft.mainsite.mybatis.model.TCmsCategory;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.soft.mainsite.module.category.service.TCmsCategoryService;


/**
 * Description:主站栏目创建选择全局后,子站点栏目管理生成栏目结构树
 *
 * @author liu
 * @create 2019-04-15
 **/
@Service
public class TreeCategoryAutomaticFunction implements Function{
	@Autowired
	private TCmsCategoryService categoryService;
	
    @Value("${system.http.protocol}")
    private String httpProtocol;

    @Value("${system.http.host}")
    private String httpHost;
	
    @Override
    public Object call(Object[] objects, Context context) {

        Long pid = Long.parseLong(objects[0].toString());
        Integer siteId = Integer.parseInt(objects[1].toString());
        return recursion(pid,siteId);
    }

    /**
     * 递归获取栏目管理栏目菜单树
     * @param pid
     * @param siteId
     * @return
     */
    private String recursion(Long pid,Integer siteId){
        StringBuffer sbf = new StringBuffer();
        List<TCmsCategory> cats  = categoryService.findCategoryListByPid(pid,siteId);
        if(cats!=null&&cats.size()>0){
            for(TCmsCategory cat:cats){
                sbf.append("  <li data-id=\""+cat.getCategoryId()+"\" data-pid=\""+pid+"\" data-url=\""+httpProtocol+"://"+ httpHost+"/system/cms/category/input?id="+cat.getCategoryId()+"\" data-divid=\"#layout-11\">"+cat.getCategoryName()+" ["+cat.getCategoryId()+"] </li>");
                sbf.append(recursion(cat.getCategoryId(),siteId));
            }
            return  sbf.toString();
        }
        return "";
     }
    
    
    
		
}
